package cn.xz.kjg.dialog

import android.view.Gravity
import android.widget.ImageView
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import cn.xz.kjg.R
import cn.xz.kjg.adapter.LocalListAdapter
import cn.xz.kjg.base.BaseApplication
import cn.xz.kjg.base.BaseDialogRxFragment
import cn.xz.kjg.bean.DeviceList
import cn.xz.kjg.bean.Flash

/**
 * 本地列表弹窗
 * */
class LocalListDialog: BaseDialogRxFragment() {

    private var listDialogClose: ImageView? = null
    private var rvLocalList: RecyclerView? = null

    private var mDevice: DeviceList? = null
    private var mList: ArrayList<Flash> = arrayListOf()

    companion object {
        fun newInstance(
            mDevice: DeviceList,
            mList: ArrayList<Flash>
        ): LocalListDialog {
            val dialog = LocalListDialog()
            dialog.mDevice = mDevice
            dialog.mList = mList
            return dialog
        }
    }

    override fun getLayoutId(): Int {
        return R.layout.dialog_local_list
    }

    override fun configDialog() {
        this.setDialogSize(gravity = Gravity.CENTER,width = 500)
    }

    override fun initViews() {
        dialog?.let {
            listDialogClose = it.findViewById(R.id.list_dialog_close)
            rvLocalList = it.findViewById(R.id.rv_local_list)
        }

        //点击外部不隐藏
        dialog?.setCancelable(false)
        dialog?.setCanceledOnTouchOutside(false)
        //数据
        initData()
        //点击事件
        initEvent()
    }

    private fun initData(){
        if (rvLocalList != null && mDevice != null){
            rvLocalList!!.layoutManager = LinearLayoutManager(BaseApplication.getContext())
            rvLocalList!!.itemAnimator = DefaultItemAnimator()
            val mAdapter = LocalListAdapter(mDevice!!)
            rvLocalList!!.adapter = mAdapter
            mAdapter.setList(mList)
        }
    }

    private fun initEvent(){
        listDialogClose?.setOnClickListener {
            dismiss()
        }
    }

}